package com.xinou.daily_report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.util.BeanUtil;
import com.xinou.daily_report.PO.daily.DailyFlowerAddPO;
import com.xinou.daily_report.PO.daily.DailyFlowerListPO;
import com.xinou.daily_report.VO.DailyFlowerInfoVO;
import com.xinou.daily_report.entity.SysDaily;
import com.xinou.daily_report.entity.SysFlower;
import com.xinou.daily_report.entity.SysIntegral;
import com.xinou.daily_report.enumerate.SysConfigEnum;
import com.xinou.daily_report.mapper.SysFlowerMapper;
import com.xinou.daily_report.service.SysDailyService;
import com.xinou.daily_report.service.SysFlowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinou.daily_report.service.SysIntegralService;
import com.xinou.daily_report.utils.ConstUtil;
import com.xinou.sso.dto.UserDTO;
import com.xinou.sso.service.SysUserService;
import com.xinou.sso.util.ShiroUtil;
import com.xinou.sso.vo.user.UserListVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-06-29
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysFlowerServiceImpl extends ServiceImpl<SysFlowerMapper, SysFlower> implements SysFlowerService {

    final SysIntegralService sysIntegralService;
    final SysDailyService sysDailyService;
    final SysUserService sysUserService;

    @Override
    public ApiResponse<String> giveFlower(DailyFlowerAddPO po) {
        if(po.getDailyId()==null) {
            throw new BusException("送花失败");
        }
        SysFlower sysFlower = BeanUtil.beanA2beanB(po, SysFlower.class).setUserId(ShiroUtil.getUserId()).setGiveDate(LocalDate.now());
        SysDaily sysDaily = sysDailyService.getById(po.getDailyId());
        if(sysDaily==null) {
            throw new BusException("日报id不存在，送花失败！");
        }
        if(sysFlower.getUserId().equals(sysDaily.getWorkerId())){
            throw new BusException("不能给自己送花！");
        }
        if(count(new LambdaQueryWrapper<SysFlower>()
                .eq(SysFlower::getUserId,sysFlower.getUserId())
                .eq(SysFlower::getGiveDate,sysFlower.getGiveDate())
        )>= SysConfigEnum.GIVE_FLOWER_MAX_TIMES.getValue()){
            throw new BusException("送花失败，超出每天最大送花次数");
        }
        sysIntegralService.save(new SysIntegral()
                .setIntegralType(ConstUtil.INTEGRAL_TYPE_FLOWER_ADD_SCORE)
                .setReason("送花加分")
                .setAddUserId(ShiroUtil.getUserId())
                .setUserId(sysDaily.getWorkerId())
                .setSaveDate(LocalDate.now())
                .setTheIntegral(1d/SysConfigEnum.DEFAULT_FLOWER_SCORE.getValue())
        );
        save(sysFlower);
        return new ApiResponse<>("送花成功!");
    }

    @Override
    public ApiResponse<Boolean> canGiveFlower() {
        return new ApiResponse<>(count(new LambdaQueryWrapper<SysFlower>()
                .eq(SysFlower::getUserId,ShiroUtil.getUserId())
                .eq(SysFlower::getGiveDate,LocalDate.now())
        )<SysConfigEnum.GIVE_FLOWER_MAX_TIMES.getValue());
    }

    @Override
    public ApiResponse<DailyFlowerInfoVO> flowerList(DailyFlowerListPO po) {
        if(po.getDailyId()==null) {
            throw new BusException("空参");
        }
        Map<Long, UserDTO> realNameByUserIds = sysUserService.getUserDtoByUserIds(sysUserService.userAll().stream().map(UserListVO::getUserId).collect(Collectors.toSet()));
        List<DailyFlowerInfoVO> dailyFlowerInfoList = BeanUtil.listA2ListB(list(new LambdaQueryWrapper<SysFlower>().eq(SysFlower::getDailyId, po.getDailyId())), DailyFlowerInfoVO.class);
        for (DailyFlowerInfoVO sysFlower : dailyFlowerInfoList) {
            UserDTO userDTO = realNameByUserIds.get(sysFlower.getUserId());
            String realName = null;
            String icon = null;
            if(userDTO!=null){
                if(userDTO.getRealName()!=null){
                    realName=userDTO.getRealName();
                }
                if(userDTO.getIcon()!=null){
                    icon = userDTO.getIcon();
                }
            }
            sysFlower.setUser(realName);
            sysFlower.setIcon(icon);
        }
        return new ApiResponse<>(dailyFlowerInfoList);
    }
}
